package com.xyazm.fulfill.api;

import com.xyazm.common.core.JsonResult;
import com.xyazm.common.utils.ParamCheckUtil;
import com.xyazm.fulfill.domain.param.CancelFulfillParam;
import com.xyazm.fulfill.domain.param.TriggerFulFillParam;
import com.xyazm.fulfill.domain.param.TriggerOrderAfterFulfillEventParam;
import com.xyazm.fulfill.exception.FulfillBizException;
import com.xyazm.fulfill.service.IFulfillService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 履约服务对外暴露的接口实现类
 * @author xuyou
 * 2022/6/8 0008 21:46
*/
@Slf4j
@DubboService(version = "1.0.0",interfaceClass = IFulfillApi.class, retries = 0)
public class FulfillApi implements IFulfillApi {

    @Autowired
    private IFulfillService fulfillService;

    @Override
    public JsonResult<Void> triggerFulFill(TriggerFulFillParam param) {
        try {
            ParamCheckUtil.checkObjectNonNull(param);
            fulfillService.triggerFulFill(param);
            return JsonResult.buildSuccess();
        } catch (FulfillBizException e) {
            log.error("biz error", e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch(Exception e) {
            log.error("system error", e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult<Void> cancelFulfill(CancelFulfillParam param) {
        try {
            ParamCheckUtil.checkObjectNonNull(param);
            fulfillService.cancelFulfill(param);
            return JsonResult.buildSuccess();
        } catch (FulfillBizException e) {
            log.error("biz error", e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch(Exception e) {
            log.error("system error", e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult<Void> triggerOrderWmsShipEvent(TriggerOrderAfterFulfillEventParam param) {
        try {
            ParamCheckUtil.checkObjectNonNull(param);
            fulfillService.triggerOrderWmsShipEvent(param);
            return JsonResult.buildSuccess();
        } catch (FulfillBizException e) {
            log.error("biz error", e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch(Exception e) {
            log.error("system error", e);
            return JsonResult.buildError(e.getMessage());
        }
    }

}
